mui.init();

var id_nums;
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");
var oldlevels;
var level_money;
var mask = mui.createMask(); //遮罩层
//页面刷新
window.addEventListener('refresh_method', function(e) { //执行刷新
	location.reload();
});
//申请运营商
window.onload = function() {
	mui.plusReady(function() {
		
		var self = plus.webview.currentWebview();
		var level = self.level;
		level_money = self.level_money;
		id_nums = self.id_num;
		$("#level").text(level);

		$(document).on("tap", "#determine", function() {
			var region = $(".region").text();
			if(!level) {
				mui.toast("请选择申请运营商等级", {
					duration: 'short',
					type: 'div' /* 是否使用h5绘制的对话框 */
				})
				return;
			} else if(region == "请选择") {
				mui.toast("请选择申请运营商地区", {
					duration: 'short',
					type: 'div' /* 是否使用h5绘制的对话框 */
				})
				return;
			} else if(oldlevels <= id_nums) {
				mui.toast("现等级高于申请等级，不可申请", {
					duration: 'short',
					type: 'div' /* 是否使用h5绘制的对话框 */
				})
				return;
			} else {
				$(".bg-model").fadeTo(300, 1)　　 //隐藏窗体的滚动条
				　　 $("body").css({
					"overflow": "hidden"
				});
			}
		})
		$(document).on("tap", "#isok", function() {
			$(".bg-model").hide();　　 //显示窗体的滚动条
			$("body").css({
				"overflow": "visible"
			});

			var receiver_state = $("#receiver_state").val();
			var receiver_city = $("#receiver_city").val();
			var receiver_district = $("#receiver_district").val();

			mui.ajax(user_url + '/api/applicationOperator', {
				data: {
					"amount": level_money, //支付金额
					"level": id_nums, //等级
					"phone": my_phone, //电话
					"receiver_city": receiver_city, //城市
					"receiver_district": receiver_district, //县
					"receiver_state": receiver_state, //省
					"sign": "string", //钥密
					"user_id": user_id //uid
				},
				dataType: 'json', //服务器返回json格式数据
				type: 'post', //HTTP请求类型
				timeout: 10000, //超时时间设置为10秒；
				contentType: "application/json;charset=UTF-8",
				success: function(data) {
					if(data.code == 10000) {
						mui.alert("申请成功", function() {
							mui.back();
						});
					}
				},
				error: function(xhr, type, errorThrown) {
					//异常处理；
					console.log(type);
					var error_msg = JSON.parse(xhr.responseText)
					mui.alert(error_msg.msg)
				}
			})
		})
	})
}

$(function() {
	mui.ajax(user_url + '/api/userinfo', {
		data: {
			'phone': my_phone,
			'sign': "string",
			'user_id': user_id
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		contentType: "application/json;charset=UTF-8",
		success: function(data) {
			if(data.code == 10000) {
				var oldlevel = data.resp.operatorString
				$("#oldlevel").text(oldlevel)
				oldlevels = data.resp.operator_id;
			} else {
				mui.alert(data.msg)
			}
		},
		error: function(xhr, type, errorThrown) {
			//异常处理；
			console.log(type);
		}
	})
})
mui('body').on('tap', '.mui-navigate-right', function() {
	window.top.location.href = this.href;
});

$(document).on('tap', '#cityResult3', function() {
	var txt = $("#level").text()
	if(!txt) {
		mui.alert("请选择运营商等级")
	} else {
		(function($, doc) {
			$.ready(function() {
				/**
				 *
				 *
				 * @param {Object} obj 对象
				 * @param {String} param 属性名
				 */
				var _getParam = function(obj, param) {
					return obj[param] || '';
				};
				var cityPicker3 = new $.PopPicker({
					layer: id_nums
				});

				cityPicker3.setData(cityData3);
				//var showCityPickerButton = doc.getElementById('showCityPicker3');
				var cityResult3 = doc.getElementById('cityResult3');
				var receiver_state = doc.getElementById('receiver_state');
				var receiver_city = doc.getElementById('receiver_city');
				var receiver_district = doc.getElementById('receiver_district');
				//showCityPickerButton.addEventListener('tap', function(event) {

				if(id_nums == 1) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text');
						receiver_state.value = _getParam(items[0], 'text')
						//							receiver_city.value = _getParam(items[1], 'text')
						//							receiver_district.value = _getParam(items[2], 'text')
					});
				} else if(id_nums == 2) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text');
						receiver_state.value = _getParam(items[0], 'text')
						receiver_city.value = _getParam(items[1], 'text')
						//							receiver_district.value = _getParam(items[2], 'text')
					});
				} else if(id_nums == 3) {
					cityPicker3.show(function(items) {
						cityResult3.innerText = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
						receiver_state.value = _getParam(items[0], 'text')
						receiver_city.value = _getParam(items[1], 'text')
						receiver_district.value = _getParam(items[2], 'text')
					});
				}
				//}, false);

			});
		})(mui, document);

	}
})

//模态框OK按钮点击事件
$(".bg-model-close").click(function() {　　
	$(".bg-model").hide();　　 //显示窗体的滚动条
	$("body").css({
		"overflow": "visible"
	});
})

//返回 
mui.back = function() {
	window.top.location.href = 'operator_center.html';
}
mui.init({
	beforeback: function() {
		//获得列表界面的webview
		var list = plus.webview.currentWebview().opener();
		//触发列表界面的自定义事件（refresh）,从而进行数据刷新
		mui.fire(list, 'refresh_method');
		//返回true，继续页面关闭逻辑
		return true;
	}
});